package com.flux.ui.activity

import android.os.Bundle
import android.os.Message

/**
 * 开屏基础类
 * Created on 2017/9/1.
 *
 * @author Yang
 */
abstract class FSplashActivity : FBaseActivity() {

    protected abstract val splashContentView: Int

    protected abstract val duration: Long

    internal var start: Long = 0
    internal var end: Long = 0
    internal var off: Long = 0

    protected abstract fun doInit(): Boolean

    protected abstract fun onXGClick(): Boolean

    protected abstract fun doJump()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(splashContentView)
        start = System.currentTimeMillis()
        end = start
    }

    override fun onResume() {
        super.onResume()
        if (!onXGClick()) {
            if (start == end && doInit()) {
                return
            }
            end = System.currentTimeMillis()

            off = duration - (end - start)
            if (off > 0) {
                sendEmptyMessageDelayed(0, off)
            } else {
                doJump()
                finish()
            }
        }
    }

    override fun handleMessage(msg: Message) {
        super.handleMessage(msg)
        doJump()
    }

    override fun onStop() {
        super.onStop()
        removeMessage(0)
    }
}